﻿function $$(id) {
    return !id ? null : document.getElementById(id);
}
// 添加js引用
function CreateScript(file) {
    head = document.getElementsByTagName('head').item(0);
    var new_element;
    new_element = document.createElement("script");
    new_element.setAttribute("type", "text/javascript");
    new_element.setAttribute("src", file);
    void (head.appendChild(new_element));
}
// 添加css引用
function CreateLink(file) {
    head = document.getElementsByTagName('head').item(0);
    var new_element;
    new_element = document.createElement("link");
    new_element.setAttribute("type", "text/css");
    new_element.setAttribute("rel", "stylesheet");
    new_element.setAttribute("href", file);
    void (head.appendChild(new_element));
}
// 创建背景层
function createBg(color, id) {
    var div = document.createElement("div");
    div.id = id;
    div.style.position = "absolute";
    div.style.top = "0px";
    div.style.left = "0px";
    div.style.width = document.body.clientWidth + "px";
    div.style.height = document.body.clientHeight + "px";
    div.style.zIndex = "998";
    div.style.background = color;

    if (window.ActiveXObject)
        div.style.filter = "alpha(opacity=20)";
    else
        div.style.opacity = "0.2";

    document.getElementsByTagName("body")[0].appendChild(div);
}

function createMoveDiv(w, h) {

    if (w > 1000) w = 1000;
    if (h > 600) h = 600;

    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.top = "50%";
    div.style.left = "50%";
    div.style.width = w + "px";
    div.style.height = h + "px";
    div.style.zIndex = "999";
    div.style.marginLeft = -(w / 2) + "px";
    div.style.marginTop = -(h / 2) + "px";
    div.style.background = "#fff";

    document.getElementsByTagName("body")[0].appendChild(div);
}

function printpreview() {
    if (window.ActiveXObject) {
        bdhtml = window.document.body.innerHTML; //获取当前页的html代码
        sprnstr = "<!--startprint-->"; //设置打印开始区域
        eprnstr = "<!--endprint-->"; //设置打印结束区域
        prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18); //从开始代码向后取html

        prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr)); //从结束代码向前取html
        window.document.body.innerHTML = prnhtml;
        window.print();
        window.document.body.innerHTML = bdhtml;
    } else {
        alert("页面打印暂时只支持IE浏览器.");
    }
}
//获得obj对象距浏览器边缘的位置 输入的obj可以是字符串或对象 如果是对象ID名 请用字符串传来 本函数适应于IE/FF
function getPos(obj) {
    if (typeof (obj) == "string") {
        obj = document.getElementById(obj);
    }
    var pos = [];
    pos[0] = obj.offsetLeft;    //X
    pos[1] = obj.offsetTop;     //Y
    while (obj = obj.offsetParent) {
        pos[0] += obj.offsetLeft;
        pos[1] += obj.offsetTop;
    }
    return pos;
}
//获得鼠标距浏览器边缘的位置
function getMousePost(event) {
    event = window.event ? window.event : event;
    var x = event.clientX + document.body.scrollLeft;
    var y = event.clientY + document.body.scrollTop;
    var pos = [];
    pos[0] = x;  //X
    pos[1] = y;  //Y
    return pos;
}
function ForDight(Dight, How) {
    /*      *     ForDight(Dight,How):数值格式化函数，Dight要      *     格式化的   数字，How要保留的小数位数。      */
    Dight = Math.round(Dight * Math.pow(10, How)) / Math.pow(10, How);
    return Dight;
}

//模拟 Request.QueryString[]
function GetQueryString(name) {
    var url = location.search;
    var res = "";
    if (url.indexOf("?") != -1) {
        var str = url.substr(1) //去掉?号 
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            if (strs[i].split("=")[0] == name) {
                res = strs[i].split("=")[1];
                break;
            }
        }
    }
    return res;
}

function SetQueryString(name, value, url) {
    if (url == null || url == "") {
        url = location.search;
    }
    else {
        if (url.indexOf("?") != -1) {
            url = "?" + url.split("?")[1];
        }
    }
    var res = "";
    if (url.indexOf("?") != -1) {
        if (url.indexOf(name + "=") != -1) {
            var str = url.substr(1) //去掉?号
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                if (strs[i].split("=")[0] == name)
                    res += "&" + name + "=" + value;
                else
                    res += "&" + strs[i];
            }
            if (res != "") {
                res = "?" + res.substr(1);
            }
        } else {
            res = url + "&" + name + "=" + value;
        }
    } else {
        res = "?" + name + "=" + value;
    }
    return location.pathname + res;
}
// 去空格
function removeSpace(v) {
    return v.replace(/(^\s*)|(\s*)|(\s*$)/g, "");
}
// 动态添加表单 并提交数据
function postbyform(actionname, keys, values) {
    var frm = document.createElement("form");
    frm.setAttribute("action", actionname);
    frm.setAttribute("method", "post");

    var hiddens = keys.split('|');
    if (hiddens.length > 0) {
        var vs = values.split('|');
        if (hiddens.length == vs.length) {
            for (var i = 0; i < hiddens.length; i++) {
                var hidden = document.createElement("input");
                hidden.setAttribute("name", hiddens[i]);
                hidden.value = vs[i];

                frm.appendChild(hidden);
            }
        }
    }

    document.body.appendChild(frm);
    frm.submit();
}


/**   
* @param {} obj 当前对象，一般是使用this引用。   
* @param {} vrl 主页URL   
*/
function SetHome(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)';
        obj.setHomePage(vrl);
    } catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager
                        .enablePrivilege("UniversalXPConnect");
            } catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1']
                    .getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
}
/**   
* @param {} sURL 收藏链接地址   
* @param {} sTitle 收藏标题   
*/
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    } catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        } catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}


// 查看试题大图片 开始
//
function showbigimgks(_src) {
    preJsGetImageSize();
    _src = _src.replace("http://ksimg.jxedt.com", "http://vipimg.jxedt.com")
    JsGetImageSize(_src, imgcallbakc);
}
function JsGetImageSize(sUrl, fCallback) {
    var img = new Image();
    if (! -[1, ]) {
        sUrl = sUrl + '?t=' + Math.random(); //IE下，ajax会缓存，导致onreadystatechange函数没有被触发，所以需要加一个随机数
        img.src = sUrl;
        img.onreadystatechange = function() {
            if (this.readyState == "loaded" || this.readyState == "complete") {
                fCallback({ width: img.width, height: img.height, url: sUrl });
            }
        };
    }
    else {
        img.src = sUrl;
        img.onload = function() {
            fCallback({ width: img.width, height: img.height, url: sUrl });
        };
    }
}
function imgcallbakc(j) {
    successJsGetImageSize();
    var _showwidth = 100;
    if ((j.width + 20) > 100) {
        _showwidth = j.width + 20;
    }
    ShowDialogo.show('查看原图', (j.height + 20) + 'px', (_showwidth) + 'px', '<div style="padding:10px 10px 10px 8px;"><img src="' + j.url + '"/></div>', '', 'nowurl', '0', null);
    document.onclick = function(evt) {
        var evt = window.event ? window.event : evt, target = evt.srcElement || evt.target;
        var _ishidden = true;
        while (target.nodeName.toLowerCase() != "html") {
            target = target.parentNode;
            if (typeof (target.id!='undefined') && target.id == "append_parent") {
                _ishidden = false;
                break;
            }
        }
        if (_ishidden) {
            successJsGetImageSize();
            document.onclick = function() { };
        }
    }
}
function preJsGetImageSize() {
    successJsGetImageSize();
    ShowDialogo.show('加载中...', '40px', '100px', '<div style="width:100px;height:40px;text-align:center;line-height:40px;">loading...</div>', '', 'nowurl', '0', null);
}
function successJsGetImageSize() {
    hideshowlogindiv('append_parent', 'nowurl');
}
// 查看试题大图片 结束